﻿@inject IProcessService ProcessService
@inject DialogService DialogService

<RadzenStack Gap="1.5rem">
    <RadzenTemplateForm TItem="TechnologyDetailEditRequestDto" Data="@_editRequestDto" Submit="OnSubmit">
        <RadzenStack class="rz-mb-2">
            <RadzenRow>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="工序编码" Style="width: 100%;">
                        <RadzenDropDown TValue="int" AllowClear="true" Value="_editRequestDto.ProcessId" ValueChanged="ProcessChanged"
                                        AllowFiltering="true"
                                        Data=@_processes TextProperty="Code" ValueProperty="Id" />
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="排序" Style="width: 100%;">
                        <RadzenNumeric @bind-Value="_editRequestDto.Sort"></RadzenNumeric>
                    </RadzenFormField>
                </RadzenColumn>
                <RadzenColumn Size="12" SizeSM="6">
                    <RadzenFormField Text="耗时" Style="width: 100%;">
                        <RadzenNumeric @bind-Value="_editRequestDto.TakeTime"></RadzenNumeric>
                    </RadzenFormField>
                </RadzenColumn>
            </RadzenRow>
        </RadzenStack>
        <RadzenStack Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.End" Gap="0.5rem">
            <RadzenButton ButtonStyle="ButtonStyle.Light" Text="Cancel" Variant="Variant.Flat" Click="@CancelButtonClick" />
            <RadzenButton ButtonStyle="ButtonStyle.Primary" ButtonType="ButtonType.Submit" Icon="save" Text="Save" Variant="Variant.Flat" />
        </RadzenStack>
    </RadzenTemplateForm>
</RadzenStack>

@code {

    private IEnumerable<ProcessInfoResponseDto> _processes = new List<ProcessInfoResponseDto>();
    private TechnologyDetailEditRequestDto _editRequestDto = new();

    [Parameter]
    public TechnologyDetailEditRequestDto? Model { get; set; }

    // private async Task LoadData(LoadDataArgs args)
    // {
    //     var result = await ProcessService.QueryProcesses(new ProcessSearchRequestDto());
    //     if (result != null && result.Succeeded)
    //     {
    //         _processes = result.Data!;
    //     }
    // }

    void ProcessChanged(int item)
    {
        var processInfoResponseDto = _processes.FirstOrDefault(i => i.Id == item);
        if (processInfoResponseDto != null)
        {
            _editRequestDto.Process = processInfoResponseDto;
        }

    }

    protected override async Task OnInitializedAsync()
    {
        _editRequestDto = Model ?? new();
        await base.OnInitializedAsync();

        var result = await ProcessService.QueryProcesses(new ProcessSearchRequestDto());
        if (result != null && result.Succeeded)
        {
            _processes = result.Data!;
        }
    }

    void CancelButtonClick()
    {
        DialogService.Close();
    }

    void OnSubmit()
    {
        DialogService.Close(_editRequestDto);
    }

}